import { Controller, Inject, Get, Post, Body, Query } from '@midwayjs/core';
import { WristbandAdvService } from '@service/firmware/wristband_adv.service';
import { WristbandAdv } from '@entity/firmware/wristband_adv.entity';
import { ListWristbandDevDTO } from '@dto/firmware/wristband_dev.dto';
import { resBuild } from '@utils/resBuild';

@Controller('/wristband_adv')
export class WristbandAdvController {
  @Inject()
  wristbandAdvService: WristbandAdvService;

  @Get('/list')
  async getAll(@Query('mac') mac?: string) {
    try {
      let data = await this.wristbandAdvService.getAll(mac);
      return resBuild.data(data)
    } catch (err) {
      console.log(err,'err')
      return resBuild.fail("查找失败！")
    }
  }



  @Get('/list_wb')
  async getWbAll(@Query() queryParams: ListWristbandDevDTO) {
    console.log('date',queryParams)
    try {
      let data = await this.wristbandAdvService.getWbAll(queryParams);
      return resBuild.data(data)
    } catch (err) {
      console.log(err,'err')
      return resBuild.fail("查找失败！", err)
    }
  }


  // 根据手环mac取实时数据的最后一条数据
  @Get('/list_wb_last')
  async getWbLast(@Query() queryParams) {
    try {
      let data = await this.wristbandAdvService.getWbLast(queryParams);
      return resBuild.data(data)
    } catch (err) {
      console.log(err,'err')
      return resBuild.fail("查找失败！", err)
    }
  }

  // 手动删除3天前的数据
  @Get('/deleteData')
  async deleteData(){
    await this.wristbandAdvService.deleteData()
  }



  @Get('/list_wb_data')
  async getWbData(@Query() queryParams: ListWristbandDevDTO) {
    try {
      let data = await this.wristbandAdvService.getWbData(queryParams);
      return resBuild.data(data)
    } catch (err) {
      console.log(err,'err')
      return resBuild.fail("查找失败！", err)
    }
  }

  // 设置关联 gw_mac 与 指定 mac 关联至 gateway_wristband 表 绑定接口
  @Post('/gateway_wristband')
  async setGatewayWristband(@Body() body) {
    try {
    await this.wristbandAdvService.setGatewayWristband(body);
    return resBuild.success("绑定成功！")
    }catch (err) {
      return resBuild.fail("绑定失败！")
    }
  }
  

  // 解绑接口
  @Post('/unsetGatewayWristband')
  async unsetGatewayWristband(@Body() body){
    try {
      await this.wristbandAdvService.unsetGatewayWristband(body);
      return resBuild.success("解绑成功！")
      }catch (err) {
        return resBuild.fail("解绑失败！", err)
      }
  }

  @Post('/wristband')
  async create(@Body() gw_mac, mac, rssi, data: Partial<WristbandAdv>, format, date) {
    return this.wristbandAdvService.createAdv(gw_mac, mac, rssi, data, format, date);
  }
}
